//
// PepServer
//
// Copyright (C) 2007 Brian Stevens.
//
//  This file is part of the JavaPepper Library.
//
//  The JavaPepper Library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  The JavaPepper Library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with the JavaPepper Library; if not, write to the Free
//  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
//  02111-1307 USA.
//

package net.jextra.javapepper;

import java.io.*;
import java.util.*;

public class PepServer
{
    // ============================================================
    // Fields
    // ============================================================

    private HashMap<PepMethod, ArrayList<Route>> routes;

    // ============================================================
    // Constructors
    // ============================================================

    public PepServer()
    {
        routes = new HashMap<PepMethod, ArrayList<Route>>();

        for ( PepMethod method : PepMethod.values() )
        {
            routes.put( method, new ArrayList<Route>() );
        }
    }

    // ============================================================
    // Methods
    // ============================================================

    // ----------
    // public
    // ----------

    public void get( String pattern, PepResponder responder )
    {
        addRoute( PepMethod.GET, pattern, responder );
    }

    public void post( String pattern, PepResponder responder )
    {
        addRoute( PepMethod.POST, pattern, responder );
    }

    public void processRequest( PepRequest request, PepResponse response )
        throws Exception
    {
        for ( Route route : routes.get( request.getMethod() ) )
        {
            if ( matches( route, request ) )
            {
                route.getResponder().processRequest( request, response );
            }
        }
    }

    // ----------
    // private
    // ----------

    private void addRoute( PepMethod method, String pattern, PepResponder responder )
    {
        routes.get( method ).add( new Route( pattern, responder ) );
    }

    private boolean matches( Route route, PepRequest request )
    {
        // TODO
        return true;
    }

    // ============================================================
    // Inner Classes
    // ============================================================

    private class Route
    {
        private String pattern;
        private PepResponder responder;

        public Route( String pattern, PepResponder responder )
        {
            this.pattern = pattern;
            this.responder = responder;
        }

        public String getPattern()
        {
            return pattern;
        }

        public PepResponder getResponder()
        {
            return responder;
        }
    }
}
